package org.xtext.runtime.interpreter;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.xtext.runtime.formula.Predicate;
import org.xtext.runtime.plan.IPlanStep;
import org.xtext.runtime.term.Variable;
import org.xtext.runtime.util.Bindings;

public class Plan {
	private IPlanStep plan;
	private Predicate label;

	public Plan(Predicate label, IPlanStep plan) {
		this.label = label;
		this.plan = plan;
	}
	
	public Predicate getLabel() {
		return label;
	}
	
	public IPlanStep getPlan() {
		return plan;
	}
	
	public Plan apply(Bindings bindings) {
		return new Plan((Predicate) label.apply(bindings), plan.apply(bindings));
	}
	
	public String toString() {
		return "plan " + label + plan.toString(IPlanStep.TAB) + "\n";
	}
	
	public String toFullString() {
		return "plan " + label + plan.toFullString(IPlanStep.TAB) + "\n";
	}

	public Set<Variable> getVariables() {
		Set<Variable> variables = label.variables();
		variables.addAll(plan.variables());
		return variables;
	}

	public Plan copy() {
		Map<String, Variable> vars = new HashMap<String, Variable>();
		return new Plan(label.copy(vars), plan.copy(vars));
	}
}
